package com.tc.travel.submitter.service;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tc.travel.submitter.pojo.VisaForDownloadReturnReportDto;
import com.tc.travel.submitter.pojo.VisaForImportReturnReportDto;
import com.tc.travel.util.HttpClientUtil;
import com.tc.travel.util.Json;
import com.tc.travel.util.PropertyUtil;
import com.tc.travel.util.StringUtil;
import com.tc.travel.util.exception.ComRuntimeException;

@Component
public class ReturnReportBo {
	private static Logger log = Logger.getLogger(ReturnReportBo.class);

	// 获取要导入归国报告的visa
	public VisaForImportReturnReportDto getVisaForImportReturnReport() {
		String url = PropertyUtil.interfaceUrlVisaGetForImportReturnReport;
		Map<String, String> params = new HashMap<String, String>();
		Map<String, String> headers = new HashMap<String, String>();

		String jsonStr = HttpClientUtil.doGet(url, params, headers);

		if (StringUtil.isEmpty(jsonStr)) {
			return null;
		}

		Json<VisaForImportReturnReportDto> json = toVisaForImportReturnReportDto(jsonStr);

		if (json.isSuccess()) {
			return json.getObj();
		} else {
			throw new ComRuntimeException("getVisaForImportReturnReport error: " + json.getMsg());
		}
	}

	public Json<VisaForImportReturnReportDto> toVisaForImportReturnReportDto(String jsonStr) {
		Type jsonType = new TypeToken<Json<VisaForImportReturnReportDto>>() {
		}.getType();
		Json<VisaForImportReturnReportDto> json = new Gson().fromJson(jsonStr, jsonType);
		return json;
	}

	// 更新归报状态
	public void updateReturnReportState(Long visaInfoId, int returnReportState, String errorMsg) {
		String url = PropertyUtil.interfaceUrlVisaUpdateReturnReportState;
		Map<String, String> params = new HashMap<String, String>();
		params.put("visaInfoId", visaInfoId + "");
		params.put("returnReportState", returnReportState + "");
		params.put("errorMsg", errorMsg);

		Map<String, String> headers = new HashMap<String, String>();

		String jsonStr = HttpClientUtil.doPost(url, params, headers);
		log.info(jsonStr);
	}

	// 获取可以下载归报的visa
	public VisaForDownloadReturnReportDto getVisaForDownloadReturnReport() {
		String url = PropertyUtil.interfaceUrlVisaGetForDownloadReturnReport;
		Map<String, String> params = new HashMap<String, String>();
		Map<String, String> headers = new HashMap<String, String>();

		String jsonStr = HttpClientUtil.doGet(url, params, headers);

		if (StringUtil.isEmpty(jsonStr)) {
			return null;
		}

		Json<VisaForDownloadReturnReportDto> json = toVisaForDownloadReturnReportDto(jsonStr);

		if (json.isSuccess()) {
			return json.getObj();
		} else {
			throw new ComRuntimeException("getVisaForDownloadReturnReport error: " + json.getMsg());
		}
	}

	public Json<VisaForDownloadReturnReportDto> toVisaForDownloadReturnReportDto(String jsonStr) {
		Type jsonType = new TypeToken<Json<VisaForDownloadReturnReportDto>>() {
		}.getType();
		Json<VisaForDownloadReturnReportDto> json = new Gson().fromJson(jsonStr, jsonType);
		return json;
	}

	public void updateReturnReportDownloadState(Long visaInfoId, int returnReportDownloadState, String errorMsg) {
		String url = PropertyUtil.interfaceUrlVisaUpdateReturnReportDownloadState;
		Map<String, String> params = new HashMap<String, String>();
		params.put("visaInfoId", visaInfoId + "");
		params.put("returnReportDownloadState", returnReportDownloadState + "");
		params.put("errorMsg", errorMsg);

		Map<String, String> headers = new HashMap<String, String>();

		String jsonStr = HttpClientUtil.doPost(url, params, headers);
		log.info(jsonStr);
	}

}
